package me.bzcoder.mediapicker.photopicker;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import me.bzcoder.mediapicker.R;
import me.bzcoder.mediapicker.config.Constant;
import me.bzcoder.mediapicker.config.MediaPickerConfig;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.SelectionCreator;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;
import com.zhihu.matisse.ui.MatisseActivity;

import java.util.List;
import java.util.function.Consumer;


/**
 * 照片选择器
 *
 * @author : BaoZhou
 * @date : 2018/7/12 10:02
 */
public class PhotoPickUtils {
    static public void getAllSelector(final Fragment fragment, final MediaPickerConfig config) {
        if (!XXPermissions.isGranted(fragment.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            XXPermissions.with(fragment).permission(Manifest.permission.WRITE_EXTERNAL_STORAGE).request(new OnPermissionCallback() {
                @Override
                public void onGranted(List<String> permissions, boolean all) {
                    startMatisse(true, config, Matisse.from(fragment));
                }

                @Override
                public void onDenied(List<String> permissions, boolean never) {
                    Toast.makeText(fragment.getContext(), R.string.permission_request_denied, Toast.LENGTH_LONG)
                            .show();
                }
            });
        }else{
            startMatisse(true, config, Matisse.from(fragment));
        }
    }


    static public void getAllSelector(final FragmentActivity fragmentActivity, final MediaPickerConfig config) {
        if (!XXPermissions.isGranted(fragmentActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            XXPermissions.with(fragmentActivity).permission(Manifest.permission.WRITE_EXTERNAL_STORAGE).request(new OnPermissionCallback() {
                @Override
                public void onGranted(List<String> permissions, boolean all) {
                    startMatisse(true, config, Matisse.from(fragmentActivity));
                }

                @Override
                public void onDenied(List<String> permissions, boolean never) {
                    Toast.makeText(fragmentActivity, R.string.permission_request_denied, Toast.LENGTH_LONG)
                            .show();
                }
            });
        }else{
            startMatisse(true, config, Matisse.from(fragmentActivity));
        }
    }

    static public void checkPermissions(final FragmentActivity fragmentActivity, final Consumer<Boolean> callback) {
        if (!XXPermissions.isGranted(fragmentActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            XXPermissions.with(fragmentActivity).permission(Manifest.permission.WRITE_EXTERNAL_STORAGE).request(new OnPermissionCallback() {
                @Override
                public void onGranted(List<String> permissions, boolean all) {
                    if(callback!=null){
                        callback.accept(true);
                    }
                }

                @Override
                public void onDenied(List<String> permissions, boolean never) {
                    Toast.makeText(fragmentActivity, R.string.permission_request_denied, Toast.LENGTH_LONG)
                            .show();
                }
            });
        }else{
            if(callback!=null){
                callback.accept(true);
            }
        }
    }


    public static Intent getMatisseIntent(Context context, MediaPickerConfig config, Matisse matisse) {
        SelectionCreator creator = matisse.choose(config.getPhotoPickerMediaType())
                .showSingleMediaType(config.getMaxVideoSelectable() == 0 || config.getMaxImageSelectable() == 0)
                .theme(R.style.Matisse_Zhihu)
                .capture(config.capture)
                .captureStrategy(new CaptureStrategy(true,config.getCaptureAuthority(),config.getCaptureDirectory()))
                .countable(config.isCountable())
                .addFilter(new FileSizeFilter(config.getMaxWidth(), config.getMaxHeight(), config.getMaxVideoSize() * Filter.K * Filter.K, config.getMaxImageSize() * Filter.K * Filter.K, config.getMaxVideoLength()))
                .originalEnable(config.isOriginalEnable())
                .maxOriginalSize(config.getMaxOriginalSize())
                .imageEngine(config.getImageEngine())
                .imageShowEditText(config.isImageShowEditText());

        if(config.getMaxImageSelectable()+config.getMaxImageSelectable()==1){
            creator.maxSelectable(1);
        }else{
            creator.maxSelectablePerMediaType(config.getMaxImageSelectable() == 0 ? 1 : config.getMaxImageSelectable()
                    , config.getMaxVideoSelectable() == 0 ? 1 : config.getMaxVideoSelectable());
        }
        Intent intent = new Intent(context, MatisseActivity.class);
        return intent;
    }



    private static void startMatisse(Boolean aBoolean, MediaPickerConfig config, Matisse matisse) {
        if (aBoolean) {
            if (config.getImageEngine() == null) {
                throw new IllegalArgumentException("ImageEngine cannot be null");
            }
            SelectionCreator creator = matisse.choose(config.getPhotoPickerMediaType())
                    .showSingleMediaType(config.getMaxVideoSelectable() == 0 || config.getMaxImageSelectable() == 0)
                    .theme(R.style.Matisse_Zhihu)
                    .countable(config.isCountable())
                    .capture(config.capture)
                    .captureStrategy(new CaptureStrategy(true,config.getCaptureAuthority(),config.getCaptureDirectory()))
                    .addFilter(new FileSizeFilter(config.getMaxWidth(), config.getMaxHeight(), config.getMaxVideoSize() * Filter.K * Filter.K, config.getMaxImageSize() * Filter.K * Filter.K, config.getMaxVideoLength()))
                    .originalEnable(config.isOriginalEnable())
                    .maxOriginalSize(config.getMaxOriginalSize())
                    .imageEngine(config.getImageEngine());

            creator.forResult(Constant.REQUEST_CODE_CHOOSE);
        }
    }

}


